﻿#ifndef _TES_MATRIX4X4_H_
#define _TES_MATRIX4X4_H_

#include <math.h>


#ifndef PAI
#define PAI 3.1415926585f
#endif

class TesVector3;
class TesVector4;
class TesMatrix3x3;

class TesMatrix4x4
{
public:
	float m4x4[16];

	void Zero();
	TesMatrix4x4();
	TesMatrix4x4(float m[16]);
	TesMatrix4x4(const float m[16]);
	void SetMatrix(float m[16]); 
	void SetMatrix(const float m[16]);
	void SetMatrix(const TesMatrix3x3& matrix3x3);
	TesMatrix4x4(const TesMatrix3x3& matrix3x3);
	TesMatrix4x4& operator = (const TesMatrix4x4& that);
	void Identity();
	TesMatrix4x4 operator + (const TesMatrix4x4& that);
	TesMatrix4x4 operator - (const TesMatrix4x4& that);
	TesMatrix4x4 operator * (const TesMatrix4x4& that);
	TesMatrix4x4 operator * (const float& K) const;
	TesMatrix4x4 operator / (const float& K); 
	float _Cofactor(int r, int c); //代数余子式
	TesMatrix4x4 _AdjoinMatrix(); //伴随矩阵
	float Determinant();  //行列式
	TesMatrix4x4 Transpose(); //转置
	TesMatrix4x4 Inverse(); //逆
	TesMatrix4x4 SetScale(float x, float y, float z);
	TesMatrix4x4 SetRotationXAxis(float Redus);
	TesMatrix4x4 SetRotationYAxis(float Redus);
	TesMatrix4x4 SetRotationZAxis(float Redus);
	TesMatrix4x4 SetRotation(float Redus, const TesVector3& Axis);
	TesMatrix4x4 SetTransform(const TesMatrix3x3& M3x3);
	TesMatrix4x4 SetTranslation(float x, float y, float z);
	TesMatrix4x4 SetW(float Num);
	void _GetToArray(float matrix[16]);
	float& operator [] (int Index);
};

#define PosTOIndex4(x, y) ((y - 1) * 4 + x - 1)
TesVector3 operator * (const TesVector3& v, const TesMatrix4x4& matrix);
TesVector4 operator * (const TesVector4& v, const TesMatrix4x4& matrix);
TesMatrix4x4 operator * (const float& K, const TesMatrix4x4& matrix);
TesMatrix3x3 GetMatrix3x3(const TesMatrix4x4& matrix, int Row, int Column);

#define M11_4 m4x4[0]
#define M12_4 m4x4[1]
#define M13_4 m4x4[2]
#define M14_4 m4x4[3]
#define M21_4 m4x4[4]
#define M22_4 m4x4[5]
#define M23_4 m4x4[6]
#define M24_4 m4x4[7]
#define M31_4 m4x4[8]
#define M32_4 m4x4[9]
#define M33_4 m4x4[10]
#define M34_4 m4x4[11]
#define M41_4 m4x4[12]
#define M42_4 m4x4[13]
#define M43_4 m4x4[14]
#define M44_4 m4x4[15]

#endif